package com.practice.datasource;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.sql.Connection;

public class ConnectionHandler implements InvocationHandler {
    private final Connection realConnection;
    private final DBCPDataSource dataSource;

    public ConnectionHandler(Connection realConnection,DBCPDataSource dataSource){
        this.realConnection = realConnection;
        this.dataSource = dataSource;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if ("close".equals(method.getName())){
            dataSource.returnConnection((Connection) proxy);
            return null;
        }
        return method.invoke(realConnection,args);
    }
}
